// 商户账号表的接口 杨鋆
var express = require('express');
var router = express.Router();
const User = require('../db/user')
const adminUser = require('../db/adminuser')
// 注册
router.post('/regist', function (req, res, next) {
  let { username, password } = req.body
  User.find({ username }, function (err, data) {
    if (!err) {
      if (data.length == 0) {
        User.create([
          { username, password },
        ], (err, data) => {
          if (!err) {
            res.send({
              code: 200,
              msg: '注册成功',
              data
            })
          } else {
            throw err;
          }
        })
      } else {
        res.send({
          code: 201,
          msg: '用户名重复',
          data: []
        })
      }
    }
  })
});
// 登录
router.post("/login", function (req, res) {
  let { username, password } = req.body
  User.find({ username,password }, function (err, data) {
    if (!err) {
      if (data.length == 0) {
        res.send({
          code: 201,
          msg: '用户名或密码错误',
          data: []
        })
      } else {
        res.send({
          code: 200,
          msg: '登录成功',
          data
        })

      }
    }
  })
})
// 管理员登录
router.post("/adminlogin", function (req, res) {
  let { username, password } = req.body
  adminUser.find({ username,password }, function (err, data) {
    if (!err) {
      if (data.length == 0) {
        res.send({
          code: 201,
          msg: '用户名或密码错误',
          data: []
        })
      } else {
        res.send({
          code: 200,
          msg: '登录成功',
          data
        })

      }
    }
  })
})
// 修改账户信息 (yh)
router.post("/changePassword", function (req, res) {
  let { name, info, img, notice, password, username } = req.body
  User.update({ username }, { name, info, img, notice, password }, function (err, data) {
    if (!err) {
      res.send("ok")
    }
  })
})

module.exports = router;
